<?php

namespace app\admin\controller;

use app\model\ExamineLogModel;
use app\model\TeacherGetPriceModel;
use think\Db;
use think\Exception;
use think\Validate;

class Teacher extends Base
{
    /**
     * 老师提现处理
     * @return mixed
     */
    public function outMoney()
    {
        return $this->fetch('teacher/outMoney');
    }

    /**
     * 老师提现处理接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function outMoneyList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $TeacherGetPriceModel = new TeacherGetPriceModel();
        $arr = ['like'=>'user_info.ui_username','1'=>'user_info.ui_phone'];
        $where = $this->select_param($arr,$param);
        $list = $TeacherGetPriceModel->getPriceListPage($page,$pageSize,$where,$is_count = false,$order='t.created_at desc');
        $count = $TeacherGetPriceModel->getPriceListPage($page,$pageSize,$where,$is_count = true,$order='t.created_at desc');
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }

    /**
     * 记录交易号信息
     * @return mixed
     */
    public function changeOutMoney()
    {
        $param = $this->request->param();
        $this->assign('id',$param['id']);
        return $this->fetch('teacher/changeOutMoney');
    }


    /**
     * 保存交易号信息
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateOutMoney()
    {
        $param = $this->request->param();
        $rule = [
            'trans_number|交易号'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>400,'msg'=>$validate->getError()]);
        }
        $data = [
            'trans_number'=>$param['trans_number'],
            'admin_id'=>$this->uid,
            'update_at'=>afterTime(),
            'change_time'=>afterTime(),
            'status'=>2,
        ];
        $res = Db::table('teacher_get_price')->where('id',$param['id'])->update($data);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>400,'msg'=>'操作失败']);
        }
    }


    /**
     * 老师申请通道
     * @return mixed
     */
    public function applyWay()
    {
        return $this->fetch('teacher/applyWay');
    }


    /**
     * 老师申请通道接口
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function applyWayList()
    {
        $param = $this->request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;
        $ExamineLogModel = new ExamineLogModel();
        $arr = ['like'=>'t.cn_name','1'=>'t.phone','t.en_name'];
        $where = $this->select_param($arr,$param);
        $list = $ExamineLogModel->getWayListPage($page,$pageSize,$where,$is_count = false,$order='t.created_time desc');
        foreach ($list as $key => $value){
            $list[$key]['created_time'] = date('Y-m-d H:i:s', $list[$key]['created_time']);
        }
        $count = $ExamineLogModel->getWayListPage($page,$pageSize,$where,$is_count = true,$order='t.created_time desc');
        return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
    }

    /**
     * 详细资料
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function changeApply()
    {
        $param = $this->request->param();
        $info = Db::table('examine_log')->where('el_log',$param['el_log'])->find();
        $this->assign('info',$info);
        $this->assign('el_log',$param['el_log']);
        return $this->fetch('teacher/changeApply');
    }

    public function changeAdopt()
    {
        $param = $this->request->param();
        try{
            $data = [
                'updated_time'=>time()
            ];
            Db::table('examine_log')->where('el_log',$param['el_log'])->update($data);
            return json(['code'=>200,'msg'=>'操作成功']);
        }catch (Exception $e){
            return json(['code'=>400,'msg'=>$e->getMessage()]);
        }
    }
}